<template>
  <el-card class="box-card">
    <!-- 搜索框 -->
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <!-- <el-form-item label="名称" class="movement">
          <el-input
            v-model="formInline.name"
            placeholder="请输入房源标题名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" class="movement">
          <el-select v-model="formInline.status" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <!-- <el-button type="primary" @click="search()" class="movement"
            >查询</el-button
          >
          <el-button type="primary" @click="search()" class="movement"
            >重置</el-button
          > -->
          <el-button type="primary" @click="dialogVisible = true"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 列表展示 -->
    <div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
        max-height="600"
      >
        <el-table-column
          label="序号"
          align="center"
          type="index"
          :index="indexMethod"
          width="70"
        >
        </el-table-column>
        <el-table-column prop="name" align="center" label="标题">
        </el-table-column>

        <el-table-column prop="phone_number" align="center" label="创建时间">
        </el-table-column>
        <el-table-column
          prop="is_status"
          align="center"
          label="状态"
          :formatter="formatUserState"
        >
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.is_status === 1"
              type="danger"
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              >禁用</el-button
            >
            <el-button
              v-else
              type="primary"
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              >启用</el-button
            >
            <el-button
              v-if="scope.row.is_status !== 1"
              type="danger"
              size="mini"
              @click="deleteUser(scope.$index, scope.row)"
              >删除</el-button
            >
            <el-button
              type="warning"
              size="mini"
              @click="compile(scope.$index, scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[50, 100, 200, 300]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 新增员工 -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="35%"
      :before-close="handleClose"
    >
      <el-form
        :model="formLabelAlign"
        label-width="75px"
        ref="managementForm"
        :rules="rules"
      >
        <div class="oneLine">
          <el-form-item label="名称" prop="name">
            <el-input
              v-model="formLabelAlign.name"
              placeholder="请输入房源标题名称"
            ></el-input>
          </el-form-item>
        </div>
        <div class="oneLine">
          <el-form-item label="请输入隐私政策" prop="name">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="textarea"
            >
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div>
        <el-button
          type="primary"
          dialogVisible
          class="confirm"
          @click="submitData('managementForm')"
          >确定添加</el-button
        >
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  user_po_list,
  uploadFiles,
  employee,
  condition,
  userDelete,
  compileAuth,
} from "../../../utils/api";

export default {
  data() {
    return {
      textarea: "",
      options: [
        {
          value: "1",
          label: "启用",
        },
        {
          value: "2",
          label: "停用",
        },
      ],
      loading: false,
      token: sessionStorage.getItem("token"),
      imgHeader: this.Global.imgHeader,

      formInline: {
        name: "",
        phone_number: "",
        status: "",
      },
      //新增员工弹框
      dialogVisible: false,
      currentPage: 1,
      total: 0,
      limit: 10,
      page: 1,
      authority_id: 5,
      tableData: [
        {
          id: "",
          user_id: "",
          name: "",
          phone_number: "",
          identity_img1: "",
          identity_img2: "",
          authority_id: "",
          position_name: "",
          is_status: "",
          names: "",
          img: "",
          image: "",
          is_baiben: "",
          jiguan: "",
          gonling: "",
          user_skill: "",
        },
      ],
      //新增员工
      formLabelAlign: {
        name: "",
        region: "",
        type: "送水工",
      },
      //上传图片
      imageUrl: "",
      fileList: [],
      fileType: ["png", "jpg", "jpeg"],
      fileLimit: 1,
      headers: { "Content-Type": "multipart/form-data" },
      //校验
      rules: {
        name: [
          { required: true, message: "请输入房源标题名称", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    //新增员工的数据
    clearData() {
      this.$refs.managementForm.resetFields();
      this.formLabelAlign.name = "";
      this.formLabelAlign.region = "";
      this.imageUrl = "";
    },
    //关闭弹框
    handleClose(done) {
      done();
      this.clearData();
    },
    //查询
    searchUser() {
      this.loading = true;
      user_po_list(
        this.token,
        this.page,
        this.formInline.name,
        this.formInline.phone_number,
        this.limit,
        this.authority_id
      )
        .then((res) => {
          if (res.data.status) {
            this.tableData = res.data.data.data;
            this.total = res.data.data.count;
            this.tableData.forEach((item) => {
              if (item.img) {
                item.image = item.img;
                item.img = this.imgHeader + item.img;
                console.log(item.img);
              }
            });

            let timer = setTimeout(() => {
              this.loading = false;
              clearTimeout(timer);
            }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.msg);
        });
    },
    search() {
      this.page = 1;
      this.searchUser();
    },
    //分页器
    //每页多少条
    handleSizeChange(val) {
      this.limit = val;
      this.searchUser();
    },
    //第几页
    handleCurrentChange(val) {
      this.page = val;
      this.searchUser();
    },

    // 序号排序
    indexMethod(index) {
      if (this.token < this.limit) {
        return this.total - index;
      } else {
        return this.total - (this.currentPage - 1) * this.limit - index;
      }
    },

    //状态值
    conditionUser(id, status) {
      condition(this.token, id, status)
        .then((res) => {
          if (res.data.status) {
            this.searchUser();
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //启用
    handleEdit(index, row) {
      if (row.is_status === 0) {
        let is_status = 1;
        this.conditionUser(row.id, is_status);
      }
      if (row.is_status === 1) {
        let is_status = 2;
        this.conditionUser(row.id, is_status);
      }
      if (row.is_status === 2) {
        let is_status = 1;
        this.conditionUser(row.id, is_status);
      }
      this.searchUser;
    },
    //删除
    deleteUser(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (row.is_status == 2) {
            userDelete(this.token, row.id).then((res) => {
              if (res.data.status) {
                this.$message({
                  type: "success",
                  message: "删除成功",
                });
                this.search();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          } else {
            this.$message({
              type: "warning",
              message: "员工在职,无法删除",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //编辑
    compile(index, row) {
      if (row.is_status == 2) {
        let obj = JSON.parse(JSON.stringify(row));
        console.log(obj);
        this.formLabelAlign.name = obj.name;
        this.formLabelAlign.region = obj.phone_number;
        this.formLabelAlign.type = obj.position_name;
        this.imageUrl = row.img;
        this.tableData.id = obj.id;
        this.dialogVisible = true;
      } else {
        this.$message({
          type: "warning",
          message: "员工在职,无法进行编辑",
        });
      }
    },
    //新增员工
    handleRemove() {},
    //添加员工
    save() {
      employee(
        this.token,
        this.imageUrl1,
        this.formLabelAlign.region,
        this.formLabelAlign.name,
        this.authority_id
      ).then((res) => {
        if (res.data.status) {
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.clearData();
          this.searchUser();
        } else {
          if (res.data.msg == "token失效,请重新进行登录") {
            this.$router.push("/login");
            let timer = setTimeout(() => {
              this.$message.error("错误，请刷新");
            }, 5000);
          }
          this.$message.error(res.data.msg);
        }
      });
    },

    //确定添加员工的回调
    submitData(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.tableData.id) {
            compileAuth(
              this.token,
              this.imageUrl1,
              this.formLabelAlign.region,
              this.formLabelAlign.name,
              this.authority_id,
              this.tableData.id
            ).then((res) => {
              console.log(res);
              if (res.data.status) {
                this.$message({
                  message: "修改成功",
                  type: "success",

                  onClose: (res) => {},
                });
                this.search();
                this.dialogVisible = false;
                this.clearData();
                this.$refs[formName].resetFields();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          } else {
            this.dialogVisible = false;
            this.save();
            this.clearData();
          }
        }
      });
    },

    formatUserState(row) {
      let is_status = row.is_status;
      if (is_status === 1) {
        return "在职";
      } else if (is_status === 2) {
        return "离职";
      } else {
        return "未审核";
      }
    },
  },
  mounted() {
    // this.toAdd();
    this.search();
  },
};
</script>

<style scoped>
.confirm {
  margin-left: 40%;
}

.movement {
  margin-right: 10px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.oneLine {
  display: flex;
  justify-content: space-between;
  margin-bottom: 14px;
}
</style>